<template>
  <div class="main">
    <center>
      <el-table
        :data="
          tableData.filter(
            data =>
              !search ||
              data.car_number.toLowerCase().includes(search.toLowerCase())
          )
        "
        style="width: 80%;margin-top:10px"
        border
        height="600"
      >
        <el-table-column label="序号" width="180" align="center">
          <template slot-scope="scope">
            <span v-text="getIndex(scope.$index)"></span>
          </template>
        </el-table-column>
        <el-table-column label="车牌号" prop="car_number"> </el-table-column>
        <el-table-column label="省份证" prop="idcard"> </el-table-column>
        <el-table-column label="姓名" prop="name">测试 </el-table-column>
        <el-table-column label="描述"
          ><template slot-scope="scope"
            ><el-tag>{{ scope.row.describe }} </el-tag>
          </template></el-table-column
        >
        <el-table-column>
          <template slot="header" slot-scope="scope">
            <el-input
              v-model="search"
              size="mini"
              placeholder="输入关键字搜索"
            />
          </template>
          <template slot-scope="scope">
            <el-button
              type="warning"
              size="mini"
              @click="addphoto(scope.$index, scope.row)"
              >处置</el-button
            >

            <!-- 编辑时候的弹出层 -->
            <el-dialog
              title="信息录入"
              :visible.sync="dialogFormVisible"
              width="500px"
              top="200px"
            >
              <el-form :model="form">
                <el-form-item label="省份证" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.idcard"
                    auto-complete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="姓名" :label-width="formLabelWidth">
                  <el-input v-model="form.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="地址" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.address"
                    auto-complete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="原因" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.reason"
                    auto-complete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="描述" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.describe"
                    auto-complete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="真实图片">
                  <el-upload
                    class="avatar-uploader"
                    action="http://localhost:8001/file/upload"
                    :show-file-list="true"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload"
                  >
                    <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  </el-upload>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addbukongpeople"
                  >确 定</el-button
                >
              </div>
            </el-dialog>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNumber"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.totalRow"
      >
      </el-pagination>
    </center>
  </div>
</template>

<script>
import { getBukonglist } from "@/api/task.js";
import { addBuKong } from "@/api/task.js";
export default {
  data() {
    return {
      tableData: [],
      search: "",
      pageNumber: 1, //当前页
      pageSize: 10, //每页大小
      totalRow: 0, //总条数
      totalPage: 0, //总页数
      input: "",

      dialogFormVisible: false, //编辑弹出层默认为关闭
      form: {
        idcard: "",
        name: "",
        describe: "",
        address: "",
        reason: "",
        image: ""
      },
      formLabelWidth: "80px",

      imageUrl: ""
    };
  },
  methods: {
    //头像上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      this.form.image = file.name;
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isLt2M;
    },

    addbukongpeople() {
      var params = {
        idcard: this.form.idcard,
        name: this.form.name,
        address: this.form.address,
        image: this.form.image,
        reason: this.form.reason,
        describe: this.form.describe,
        id: this.form.id
      };
      addBuKong("/task/operatebukong", params).then(res => {
        if (res.code === 200) {
          location.reload();
          this.$message(res.data);
        } else if (res.code === 500) {
          this.$message(res.data);
        }
      });
    },
    addphoto(index, row) {
      this.dialogFormVisible = true;
      this.form.idcard = row.idcard;
      this.form.reason = row.describe;
      this.form.id = row.id;
    },
    //分页点击方法
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      var params = {
        pageNumber: this.pageNumber,
        pageSize: this.pageSize
      };
      this.findAll(params);
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageNumber = val;
      var params = {
        pageNumber: this.pageNumber,
        pageSize: this.pageSize
      };
      this.findAll(params);
    },
    //添加表格序号
    getIndex($index) {
      return (this.pageNumber - 1) * this.pageSize + $index + 1;
    }
  },
  //初始化的时候初始化表格大小
  created() {
    var params = {
      pageNumber: this.pageNumber,
      pageSize: this.pageSize,
      value1: this.value1
    };
    getBukonglist("/task/bukonglist", params).then(res => {
      if (res.code === 200) {
        this.tableData = res.data.list;
        this.totalRow = res.data.totalRow;
      } else if (res.code === 500) {
        this.$message(res.data);
      }
    });
  }
};
</script>
<style scoped>
.main {
  width: 100%;
  height: 680px;
}
.avatar-uploader .el-upload {
  // border: 1px dashed #d9d9d9;
  border: 1px solid red;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
